package com.liantong.demo_part2.Service.Impl;

import com.liantong.demo_part2.Mapper.PONMapper;
import com.liantong.demo_part2.Mapper.PONResourceMapper;
import com.liantong.demo_part2.Service.PONResourceService;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class PONResourceServiceImpl implements PONResourceService {

    @Autowired
    private PONResourceMapper ponResourceMapper;

    @Override
    public Map<String, Object> PON_board_rate(String deparment, String olt) {
        Map<String, Object> map = ponResourceMapper.PON_board_rate(deparment, olt);
        return map;
    }

    @Override
    public Map<String, Object> PON_port_rate(String deparment, String olt) {
        Map<String, Object> map = ponResourceMapper.PON_port_rate(deparment, olt);
        return map;
    }


    @Override
    public List<String> returnDepartment() {
        List<String> list = ponResourceMapper.returnDepartment();
        return list;
    }

    @Override
    public List<String> returnOLT(String department) {
        List<String> list = ponResourceMapper.returnOLT(department);
        return list;
    }

    @Override
    public Map<String, Object> returnTree(String department) {
        Map<String, Object> result = new LinkedHashMap<>();
        List<Map<String, Object>> list = new ArrayList<>();
        List<Map<String, Object>> oltList = ponResourceMapper.OLTTree(department);
        int total = 0;
        result.put("name", "OLT");
        for (Map<String, Object> map : oltList) {
            Map<String, Object> temp = new LinkedHashMap<>();
            String OLTName = (String) map.get("设备名称");
            int OLTNum = Integer.parseInt(map.get("number").toString());
            total += OLTNum;
            temp.put("name", OLTName);
            temp.put("value", OLTNum);
            System.out.println(temp);
            List<Map<String, Object>> list1 = new ArrayList<>();
            Map<String, Object> onePonBoardMap = new LinkedHashMap<>();
            Map<String, Object> tenPonBoardMap = new LinkedHashMap<>();
//            System.out.println(1);
            Map<String, Object> PONBoardList = ponResourceMapper.PONBoardTree(department, OLTName);
            System.out.println(PONBoardList);
            int oneGPON = PONBoardList.get("1G") == null ? 0 : Integer.parseInt(PONBoardList.get("1G").toString());
            int tenGPON = PONBoardList.get("10G") == null ? 0 : Integer.parseInt(PONBoardList.get("10G").toString());
            onePonBoardMap.put("name", "1GPON板");
            onePonBoardMap.put("value", oneGPON);
            List<Map<String, Object>> list2 = new ArrayList<>();
            tenPonBoardMap.put("name", "10GPON板");
            tenPonBoardMap.put("value", tenGPON);
            tenPonBoardMap.put("color", "#D33513");
            List<Map<String, Object>> list3 = new ArrayList<>();
//            temp.put("1GPON板",oneGPON);
//            temp.put("10GPON板",tenGPON);
            Map<String, Object> onePonPortMap = new LinkedHashMap<>();
            Map<String, Object> tenPonPortMap = new LinkedHashMap<>();
            Map<String, Object> totalList = ponResourceMapper.totalTree(department, OLTName);
            int onePONPort = totalList.get("1GPONPort") == null ? 0 : Integer.parseInt(totalList.get("1GPONPort").toString());
            int oneUser = totalList.get("1GTotalUser") == null ? 0 : Integer.parseInt(totalList.get("1GTotalUser").toString());
            int tenPONPort = totalList.get("10GPONPort") == null ? 0 : Integer.parseInt(totalList.get("10GPONPort").toString());
            int tenUser = totalList.get("10GTotalUser") == null ? 0 : Integer.parseInt(totalList.get("10GTotalUser").toString());
            onePonPortMap.put("name", "1GPON口");
            onePonPortMap.put("value", onePONPort);
            List<Map<String, Object>> list4 = new ArrayList<>();
            tenPonPortMap.put("name", "10GPON口");
            tenPonPortMap.put("value", tenPONPort);
            tenPonPortMap.put("color", "#D33513");
            List<Map<String, Object>> list5 = new ArrayList<>();
//            temp.put("1GPON口",onePONPort);
            Map<String, Object> oneGUserMap = new LinkedHashMap<>();
            Map<String, Object> tenGUserMap = new LinkedHashMap<>();
            oneGUserMap.put("name", "用户");
            oneGUserMap.put("value", oneUser);
            oneGUserMap.put("children", null);
            tenGUserMap.put("name", "用户");
            tenGUserMap.put("value", tenUser);
            tenGUserMap.put("color", "#D33513");
            tenGUserMap.put("children", null);
            list4.add(oneGUserMap);
            list5.add(tenGUserMap);
            onePonPortMap.put("children", list4);
            tenPonPortMap.put("children", list5);
            list2.add(onePonPortMap);
            list3.add(tenPonPortMap);
            onePonBoardMap.put("children", list2);
            tenPonBoardMap.put("children", list3);
            list1.add(onePonBoardMap);
            list1.add(tenPonBoardMap);
            temp.put("children", list1);
//            temp.put("1G用户数",oneUser);
////            temp.put("10GPON口",tenPONPort);
//            temp.put("10G用户数",tenUser);
            list.add(temp);
        }
        result.put("value", total);
        result.put("children", list);
        return result;
    }


}
